package com.epalmpay.controller.channel.member;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import com.epalmpay.bean.CouponBean;
import com.epalmpay.commom.BaseController;
import com.epalmpay.dto.member.*;
import com.epalmpay.entity.*;
import com.epalmpay.enumdef.ActivityEnum;
import com.epalmpay.util.AES;
import com.epalmpay.util.Const;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.epalmpay.exception.ApplicationException;
import com.epalmpay.status.Rescode;
import com.epalmpay.dto.system.PageDTO;
import com.epalmpay.dto.activity.CouponInDTO;
import com.epalmpay.dto.activity.CouponInfoListOutDTO;
import com.epalmpay.dto.activity.CouponListOutDTO;
import com.epalmpay.dto.activity.CouponOutDTO;
import com.epalmpay.dto.activity.RedPackageInDTO;
import com.epalmpay.dto.activity.RedPackageListOutDTO;
import com.epalmpay.dto.activity.RedPackageOutDTO;
import com.epalmpay.dto.member.BalanceInDTO;
import com.epalmpay.dto.member.MemberConsumeOutDTO;
import com.epalmpay.dto.member.MemberInDTO;
import com.epalmpay.dto.member.MemberListOutDTO;
import com.epalmpay.dto.member.MemberOutDTO;
import com.epalmpay.dto.member.MemberTradeDetailInDTO;
import com.epalmpay.service.activity.IActivityCouponService;
import com.epalmpay.service.activity.IActivityRedPackageService;
import com.epalmpay.service.activity.ICouponUserService;
import com.epalmpay.service.activity.IRedPackageUserService;
import com.epalmpay.service.management.IUserService;
import com.epalmpay.service.member.IMemberGradeService;
import com.epalmpay.service.member.IMemberService;
import com.epalmpay.service.operator.ISysLogService;
import com.epalmpay.service.shop.IShopService;
import com.epalmpay.util.PropertyCopyUtil;
import com.epalmpay.util.Tools;

/** 
 * 说明：会员
 * 创建人：gwn
 * 创建时间：2017-05-15
 */
@Controller
@RequestMapping(value="/manager/member")
public class MemberController extends BaseController {
	
	@Resource
	private IMemberService memberService;
	
	@Resource
	private IMemberGradeService memberGradeService;
	
	@Resource
	private IUserService userService;
	
	@Resource
	private IShopService shopService;
	
	@Resource
	private IActivityRedPackageService activityredPackageService;
	
	@Resource
	private IActivityCouponService activitycouponService;
	
	@Resource
	private IRedPackageUserService redPackageUserService;
	
	@Resource
	private ICouponUserService couponUserService;
	@Resource
    private ISysLogService sysLogService;
	/**
	 * 显示会员列表
	 * @param memberInDTO
	 * @param pageDTO
	 * @return
	 */
	@RequestMapping(value="/list")
	@ResponseBody
	public MemberListOutDTO  listMember(MemberInDTO memberInDTO,PageDTO pageDTO){
		MemberListOutDTO memberListOutDTO=new MemberListOutDTO();
		try {
			Page page=Tools.pageEmtity(pageDTO);
			//取得所有会员等级
			memberListOutDTO.setMemberGradeList(memberGradeService.queryGradeList(memberInDTO));
			
			//获取会员列表列表信息
			List<MemberOutDTO> memberOutDTOList = memberService.getMemberList(memberInDTO,page);			
			memberListOutDTO.setMemberOutDTOList(memberOutDTOList);
			memberListOutDTO.setPage(page); 
			memberListOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
			
			
			
			return memberListOutDTO;
		} catch (ApplicationException ae) { 
			memberListOutDTO.setErrorCode(ae.errCode, ae.errMsg);
			ae.printStackTrace();
		}
		 catch (Exception e) { 
			 memberListOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
			e.printStackTrace();
		}
		return memberListOutDTO;
	}		
	
	


	/**
	 * 查看会员详情  根基id huixian 
	 * @param memberOut
	 * @return
	 */
	@RequestMapping(value="/findMemberById")
	@ResponseBody
	
	public MemberOutDTO  findMemberById(MemberInDTO memberOut){
		MemberOutDTO memberOuts=new MemberOutDTO();
		Member member=new Member();
		YsOilcardMember ysOilcardMember = new YsOilcardMember();
		YsObd ysObd = new YsObd();
		try {   
			member.setId(memberOut.getId());
			ysOilcardMember.setMemberId(memberOut.getId());
			ysObd.setActivateUser(memberOut.getId());
			
			Member members=memberService.findMemberById(member);
			if (members.getGroupId()==80) {//80代表是云流的  这里要显示obd信息
				//ysOilcardMember =memberService.queryOilcardMember(ysOilcardMember);
				ysObd = memberService.queryOdbDetail(ysObd);
				memberOuts.setYsObd(ysObd);
				//memberOuts.setYsOilcardMember(ysOilcardMember);
			}		
			
			
			if(members!=null){
				PropertyCopyUtil.copyPropertiesExclude(members,memberOuts);
				memberOuts.setErrorCode(Rescode.SUCCESS, "查询成功");
				return memberOuts;
			}else{
				memberOuts.setErrorCode("-1", "未查询到记录");
			}
		} catch (ApplicationException ae) {
			memberOuts.setErrorCode(ae.errCode, ae.errMsg);
			ae.printStackTrace();
		}catch (Exception e) {
			memberOuts.setErrorCode("-1", "处理出现异常");
			e.printStackTrace();
		}
		memberOuts.setErrorCode("-2", "处理失败");
		return memberOuts;
	}

	/**
	 * 查看会员银行卡信息
	 * @param memberInDTO
	 * @return
	 */
	@RequestMapping(value="/queryMemberCard")
	@ResponseBody
	public MemberListOutDTO  queryMemberCard(MemberInDTO memberInDTO){
		MemberListOutDTO memberListOutDTO=new MemberListOutDTO();
		try {

			//获取银行卡列表信息
			List<MemberCard> memberCardList = memberService.queryMemberCardList(memberInDTO);
			memberListOutDTO.setMemberCardList(memberCardList);
			memberListOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");



			return memberListOutDTO;
		} catch (ApplicationException ae) {
			memberListOutDTO.setErrorCode(ae.errCode, ae.errMsg);
			ae.printStackTrace();
		}
		catch (Exception e) {
			memberListOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
			e.printStackTrace();
		}
		return memberListOutDTO;
	}
	/**
	 * 查看油卡信息
	 * @param memberInDTO
	 * @return
	 */
	@RequestMapping(value="/queryOilcardMember")
	@ResponseBody
	public MemberListOutDTO  queryOilcardMember(MemberInDTO memberInDTO){
		MemberListOutDTO memberListOutDTO=new MemberListOutDTO();
		try {

			//获取银行卡列表信息
			List<YsOilcardMember> oilcardMemberList = memberService.queryOilcardMember(memberInDTO);
			memberListOutDTO.setOilcardMemberList(oilcardMemberList);
			memberListOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");

			return memberListOutDTO;
		} catch (ApplicationException ae) {
			memberListOutDTO.setErrorCode(ae.errCode, ae.errMsg);
			ae.printStackTrace();
		}
		catch (Exception e) {
			memberListOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
			e.printStackTrace();
		}
		return memberListOutDTO;
	}


	/**
	 * 通过会员id查询会员  编辑时回显
	 * @param memberInDTO
	 * @return
	 */
	@RequestMapping(value="/queryMemberById")
	@ResponseBody
	public MemberOutDTO  queryMemberById(MemberInDTO memberInDTO){
		MemberOutDTO memberOutDTO = new MemberOutDTO();
		Member member=new Member();
		try {   
			member.setId(memberInDTO.getId());
			Member members=memberService.findById(member);
			if(members!=null){
				PropertyCopyUtil.copyPropertiesExclude(members,memberOutDTO);
				memberOutDTO.setErrorCode(Rescode.SUCCESS, "查询成功");
				return memberOutDTO;
			}else{
				memberOutDTO.setErrorCode("-1", "未查询到记录");
			}
		} catch (Exception e) {
			memberOutDTO.setErrorCode("-1", "处理出现异常");
			e.printStackTrace();
		}
		memberOutDTO.setErrorCode("-2", "处理失败");
		return memberOutDTO;
	}
	/**
	 * 修改会员信息
	 * @param
	 * @return
	 */
	@RequestMapping(value="/updateMember")
	@ResponseBody
	public MemberOutDTO  updateMember(MemberInDTO memberInDTO){
		MemberOutDTO memberOuts=new MemberOutDTO();
		Member members = new Member();
		try { 
			members.setId(memberInDTO.getId());
			members.setPhone(memberInDTO.getPhone());
			members.setName(memberInDTO.getName());
			if(memberInDTO.getBirthday()!=null){
				members.setBirthday(memberInDTO.getBirthday());
			}
			if(memberInDTO.getSex()!=null){
				members.setSex(memberInDTO.getSex());
			}
			if(memberInDTO.getAddress()!=null){
				members.setAddress(memberInDTO.getAddress());
			}

			members.setIdNo(memberInDTO.getIdNo());
			members.setGmtModified(new Date());
			memberService.editMember(members);
			memberOuts.setErrorCode(Rescode.SUCCESS, "修改成功");
			
			String content ="修改会员信息";
			sysLogService.addSysLog(memberInDTO.getUserId(),memberInDTO.getUserName(),null,content,memberInDTO.getGroupId());
			
			return memberOuts;
		} catch (ApplicationException ae) {
			memberOuts.setErrorCode(ae.errCode, ae.errMsg);
			ae.printStackTrace();
		}catch (Exception e) {
			memberOuts.setErrorCode("-1", "处理出现异常");
			e.printStackTrace();
		}
		return null;
	}
	

	/**
	 * 通过会员id查询会员等级  编辑详情页面的会员等级时回显
	 * @param memberInDTO
	 * @return
	 */
	@RequestMapping(value="/queryMemberGrade")
	@ResponseBody
	public MemberListOutDTO  queryMemberGradeById(MemberInDTO memberInDTO){
		MemberListOutDTO memberListOutDTO=new MemberListOutDTO();
		try {
			//取得所有会员等级
			memberListOutDTO.setMemberGradeList(memberGradeService.getGradeList(memberInDTO));
			//获取会员等级信息
			List<MemberOutDTO> memberOutDTOList=memberService.findMemberGradeById(memberInDTO.getId());
			memberListOutDTO.setMemberOutDTOList(memberOutDTOList);
			memberListOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
			
			return memberListOutDTO;
		} catch (ApplicationException ae) { 
			memberListOutDTO.setErrorCode(ae.errCode, ae.errMsg);
			ae.printStackTrace();
		}
		 catch (Exception e) { 
			 memberListOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
			e.printStackTrace();
		}
		return memberListOutDTO;
	}	
	
	/**
	 * 修改会员等级信息
	 * @param memberInDTO
	 * @return
	 */
	@RequestMapping(value="/member/updateMemberGrade")
	@ResponseBody
	public MemberOutDTO  updateMemberGrade(MemberInDTO memberInDTO){
		MemberOutDTO memberOuts=new MemberOutDTO();
		Member member = new Member();
		try {

			MemberGrade memberGrade = memberGradeService.queryByUpdateAmount(memberInDTO.getGradeId());

			member.setId(memberInDTO.getId());
			member.setGradeId(memberInDTO.getGradeId());
			member.setUpdateAmount(memberGrade.getUpdateAmount());
			member.setGmtModified(new Date());
			memberService.updateMember(member);
			memberOuts.setErrorCode(Rescode.SUCCESS, "修改成功");
			
			String content ="修改会员等级信息";
			sysLogService.addSysLog(memberInDTO.getUserId(),memberInDTO.getUserName(),null,content,memberInDTO.getGroupId());
			return memberOuts;
		}catch (ApplicationException ae) {
			logger.error(ae);
			memberOuts.setErrorCode(Rescode.DEFAULT_PREFIX, ae.getMessage());
		} catch (Exception e) {
			memberOuts.setErrorCode("-1", "处理出现异常");
			e.printStackTrace();
		}
		return null;
	}
	
	
	
	
	/**
	 * 通过会员id查询会员登入 编辑详情时页面回显
	 * @param memberInDTO
	 * @return
	 */
	@RequestMapping(value="/goEditLoginPassword")
	@ResponseBody
	public MemberOutDTO  updateLoginPassword(MemberInDTO memberInDTO){
		MemberOutDTO memberOutDTO = new MemberOutDTO();
		Member member=new Member();
		try {   
			member.setId(memberInDTO.getId());
			Member members=memberService.findPasswordById(member);
			if(members!=null){
				PropertyCopyUtil.copyPropertiesExclude(members,memberOutDTO);
				memberOutDTO.setErrorCode(Rescode.SUCCESS, "查询成功");
				return memberOutDTO;
			}else{
				memberOutDTO.setErrorCode("-1", "未查询到记录");
			}
		} catch (ApplicationException ae) {
			logger.error(ae);
			memberOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, ae.getMessage());
		}catch (Exception e) {
			memberOutDTO.setErrorCode("-1", "处理出现异常");
			e.printStackTrace();
		}
		return memberOutDTO;
	}
	
	
	/**
	 * 查询账户余额
	 * @param memberInDTO
	 * @return
	 */
	@RequestMapping(value="/queryAmount")
	@ResponseBody
	public MemberOutDTO qeryAmount(MemberInDTO memberInDTO){
		MemberOutDTO memberOutDTO=new MemberOutDTO();
		try {
			memberOutDTO =memberService.queryAmount(memberInDTO);
			 memberOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
			return memberOutDTO;
		}catch (ApplicationException ae) {
			logger.error(ae);
			memberOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, ae.getMessage());
		} catch (Exception e) {
			memberOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
			e.printStackTrace();
		}
		return memberOutDTO;
	}
	


	@RequestMapping(value="/addBalance")
	@ResponseBody
	public MemberOutDTO addBalance(BalanceInDTO balanceInDTO) {
		MemberOutDTO groupOutDTO = new MemberOutDTO();
		try {
			groupOutDTO=memberService.addBalance(balanceInDTO,ROOT_HOME_URL);
			return groupOutDTO;
		}catch (ApplicationException ae) {
			logger.error(ae);
			groupOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, ae.getMessage());
		}catch (Exception e) {
			groupOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
			e.printStackTrace();
		}
		return groupOutDTO;

	}

	/**
	 * 重置支付密码
	 * @param memberInDTO
	 * @return
	 */
	@RequestMapping(value="/reSet")
	@ResponseBody
	public MemberOutDTO reSet(MemberInDTO memberInDTO){
		MemberOutDTO memberOutDTO=new MemberOutDTO();
		try {
			Member member =memberService.reSet(memberInDTO,ROOT_HOME_URL);
			memberOutDTO.setMember(member);
			memberOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
			return memberOutDTO;
		}catch (ApplicationException ae) {
			logger.error(ae);
			memberOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, ae.getMessage());
		}catch (Exception e) {
			memberOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
			e.printStackTrace();
		}
		return memberOutDTO;
	}
	
	
	/**
	 * 修改会员登入密码
	 * @param memberInDTO
	 * @return
	 */						
	@RequestMapping(value="/updateMemberLoginPassword")
	@ResponseBody
	public MemberOutDTO  updateMemberLoginPassword(MemberInDTO memberInDTO){
		MemberOutDTO memberOuts=new MemberOutDTO();
		Member member = new Member();
		Userbase userbase = new Userbase();
		
		try {   
			member.setId(memberInDTO.getId());
			userbase.setId(memberService.findById(member).getUserId());
			Userbase us=userService.findById(userbase);
//			userbase.setPassword(new SimpleHash("SHA-1", us.getUsername(), memberInDTO.getPassword()).toString());//密码加密
			userbase.setPassword(AES.AESEncode(Const.AceKey, memberInDTO.getPassword()));//密码加密
			userbase.setGmtModified(new Date());
			userService.updateLoginPassword(userbase,ROOT_HOME_URL);
			memberOuts.setErrorCode(Rescode.SUCCESS, "修改成功");
			
			String content ="修改会员登入密码";
			sysLogService.addSysLog(memberInDTO.getUserId(),memberInDTO.getUserName(),null,content,memberInDTO.getGroupId());
			return memberOuts;
		} catch (Exception e) { 
			memberOuts.setErrorCode("-1", "处理出现异常");
			e.printStackTrace();
		}
		memberOuts.setErrorCode("-2", "处理失败");
		return null;
	}
	
	
	/**
	 * 去批量发红包(会员管理)
	 * @param redpackage
	 * @return
	 */
	@RequestMapping(value="/redPackageList")
	@ResponseBody
	public RedPackageListOutDTO  redPackageList(Redpackage redpackage){
		
		RedPackageListOutDTO redPackageListOutDTO=new RedPackageListOutDTO();
		try {
			redpackage.setPurposeType(ActivityEnum.PurposeType.Member.getType());
			List<Redpackage> redPackageList=activityredPackageService.redPackageList(redpackage);
			redPackageListOutDTO.setRedpackageList(redPackageList);
			redPackageListOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
			return redPackageListOutDTO;
			
		} 
		catch (ApplicationException ae) { 
			redPackageListOutDTO.setErrorCode(ae.errCode, ae.errMsg);
			ae.printStackTrace();
		}
		 catch (Exception e) { 
			 redPackageListOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
			e.printStackTrace();
		}
		return redPackageListOutDTO;
	}	
	

	/**
	 * 批量发红包
	 * @param redPackageInDTO
	 * @return
	 */						
	@RequestMapping(value="/addPackageList")
	@ResponseBody
	public RedPackageOutDTO  addPackageList(RedPackageInDTO redPackageInDTO){

		RedPackageOutDTO redPackageOutDTO=new RedPackageOutDTO();
		try {
            int result = 0;


//			redPackageUserService.saveRedPackage(redPackageInDTO);

			//单钩和全选的  改动
			Long redpackageId = redPackageInDTO.getRedpackageId();
			Integer getType = redPackageInDTO.getGetType();

			String message0="";
			String msgMemberId1="";
			String msgMemberId2="";
			String msgMemberId3="";
			String msgMemberId4="";
			String msgMessage1="";
			String msgMessage2="";
			String msgMessage3="";
			String msgMessage4="";
			int succCount = 0;
			int  failCount= 0;
			try {
				if (redPackageInDTO.getMemberString() != null) {
					if (redPackageInDTO.getMemberString().length() == 1) {
						Long id = Long.parseLong(redPackageInDTO.getMemberString());
						result = redPackageUserService.sendOneMemberRedPackage(id, redpackageId, null,
								ActivityEnum.RedPackageGetType.DirectedRelease.getType(),null);
						if(result ==1){
							succCount += 1;
						}else if(result ==-1){
							msgMemberId1 += id+",";
							msgMessage1="红包已结束,,会员号为:";
						}else if (result ==-2){
							msgMemberId2 += id+",";
							msgMessage2="红包已领完,会员号为:";
						}else if (result ==-3){
							msgMemberId3 += id+",";
							msgMessage3="过了结束领取时间,会员号为:";
						}else if (result ==-4){
							msgMemberId4 += id+",";
							msgMessage4="超过最大领取张数,会员号为:";
						}
						if(result ==-1 ||result ==-2 ||result ==-3 ||result ==-4){
							failCount +=1;
							 message0=",错误原因:";
						}


					} else {
						String[] ids = redPackageInDTO.getMemberString().split(",");
						for (String id : ids) {
							result = redPackageUserService.sendOneMemberRedPackage(Long.parseLong(id), redpackageId, null,
									ActivityEnum.RedPackageGetType.DirectedRelease.getType(),null);
							if(result ==1){
								succCount += 1;
							}else if(result ==-1){
								msgMemberId1 += id+",";
								msgMessage1="红包已结束,,会员号为:";
							}else if (result ==-2){
								msgMemberId2 += id+",";
								msgMessage2="红包已领完,会员号为:";
							}else if (result ==-3){
								msgMemberId3 += id+",";
								msgMessage3="过了结束领取时间,会员号为:";
							}else if (result ==-4){
								msgMemberId4 += id+",";
								msgMessage4="超过最大领取张数,会员号为:";
							}
							if(result ==-1 ||result ==-2 ||result ==-3 ||result ==-4){
								failCount +=1;
								message0=",错误原因:";
							}
						}
					}
				} else {//按条件选
					List<Long> memberList = memberService.getMembers(redPackageInDTO);
					for (Long id : memberList) {
						result = redPackageUserService.sendOneMemberRedPackage(id, redpackageId, null, getType,null);
						if(result ==1){
							succCount += 1;
						}else if(result ==-1){
							msgMemberId1 += id+",";
							msgMessage1="红包已结束,,会员号为:";
						}else if (result ==-2){
							msgMemberId2 += id+",";
							msgMessage2="红包已领完,会员号为:";
						}else if (result ==-3){
							msgMemberId3 += id+",";
							msgMessage3="过了结束领取时间,会员号为:";
						}else if (result ==-4){
							msgMemberId4 += id+",";
							msgMessage4="超过最大领取张数,会员号为:";
						}
						if(result ==-1 ||result ==-2 ||result ==-3 ||result ==-4){
							failCount +=1;
							message0=",错误原因:";
						}
					}
				}

			}catch (Exception ae){
				logger.error(ae);
			}
			if(msgMemberId1 !="" && msgMemberId1 !=null  ){
				redPackageOutDTO.setMsgMemberId1(msgMemberId1.substring(0,msgMemberId1.length()-1));
			}else{
				redPackageOutDTO.setMsgMemberId1("");
			}
			if(msgMemberId2 !="" && msgMemberId2 !=null ) {
				redPackageOutDTO.setMsgMemberId2(msgMemberId2.substring(0, msgMemberId2.length() - 1));
			}else{
				redPackageOutDTO.setMsgMemberId2("");
			}
			if(msgMemberId3 !="" && msgMemberId3 !=null ) {
				redPackageOutDTO.setMsgMemberId3(msgMemberId3.substring(0, msgMemberId3.length() - 1));
			}else{
				redPackageOutDTO.setMsgMemberId3("");
			}
			if(msgMemberId4 !="" && msgMemberId4 !=null ) {
				redPackageOutDTO.setMsgMemberId4(msgMemberId4.substring(0, msgMemberId4.length() - 1));
			}else{
				redPackageOutDTO.setMsgMemberId4("");
			}
			if(msgMessage1 !="" && msgMessage1 !=null  ){
				redPackageOutDTO.setMsgMessage1(msgMessage1);
			}else{
				redPackageOutDTO.setMsgMessage1("");
			}
			if(msgMessage2 !="" && msgMessage2 !=null ) {
				redPackageOutDTO.setMsgMessage2(msgMessage2);
			}else{
				redPackageOutDTO.setMsgMessage2("");
			}
			if(msgMessage3 !="" && msgMessage3 !=null ) {
				redPackageOutDTO.setMsgMessage3(msgMessage3);
			}
			else{
				redPackageOutDTO.setMsgMessage3("");
			}
			if(msgMessage4 !="" && msgMessage4 !=null ) {
				redPackageOutDTO.setMsgMessage4(msgMessage4);
			}else{
				redPackageOutDTO.setMsgMessage4("");
			}
			if(message0 !="" && message0 !=null ) {
				redPackageOutDTO.setMessage0(message0);
			}else{
				redPackageOutDTO.setMessage0("");
			}

			redPackageOutDTO.setSuccCount(succCount);
			redPackageOutDTO.setFailCount(failCount);

			redPackageOutDTO.setErrorCode(Rescode.SUCCESS, "操作成功");
			String content ="会员批量发红包";
			sysLogService.addSysLog(redPackageInDTO.getUserId(),redPackageInDTO.getUserName(),null,content,redPackageInDTO.getGroupId());
			return redPackageOutDTO;
		} catch (Exception e) {
			redPackageOutDTO.setErrorCode("-1", "处理出现异常");
			e.printStackTrace();
		}
		redPackageOutDTO.setErrorCode("-2", "处理失败");
		return null;
	}
	
	/**
	 * 去批量优惠券(会员管理)
	 * @param coupon
	 * @return
	 */
	@RequestMapping(value="/couponList")
	@ResponseBody
	public CouponInfoListOutDTO  couponList(Coupon coupon){
		
		CouponInfoListOutDTO couponListOutDTO=new CouponInfoListOutDTO();
		try {
			coupon.setPurposeType(ActivityEnum.PurposeType.Member.getType());
			List<CouponBean> couponList=activitycouponService.couponList(coupon);
			couponListOutDTO.setCouponsList(couponList);
			
			couponListOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
		
			return couponListOutDTO;
		} 
		catch (ApplicationException ae) { 
			couponListOutDTO.setErrorCode(ae.errCode, ae.errMsg);
			ae.printStackTrace();
		}
		 catch (Exception e) { 
			 couponListOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
			e.printStackTrace();
		}
		return couponListOutDTO;
		
	}
	
	
	
	/**
	 * 批量优惠卷
	 * @param couponInDTO
	 * @return
	 */						
	@RequestMapping(value="/addCouponList")
	@ResponseBody
	public CouponOutDTO  addCouponList(CouponInDTO couponInDTO){

		CouponOutDTO couponOutDTO=new CouponOutDTO();

		try {
			int result = 0;


//			redPackageUserService.saveRedPackage(redPackageInDTO);

			//单钩和全选的  改动
			//单钩和全选的  改动
			Long couponId = couponInDTO.getCouponId();
			Integer getType = couponInDTO.getGetType();

			String message0="";
			String msgMemberId1="";
			String msgMemberId2="";
			String msgMemberId3="";
			String msgMemberId4="";
			String msgMemberId5="";
			String msgMessage1="";
			String msgMessage2="";
			String msgMessage3="";
			String msgMessage4="";
			String msgMessage5="";
			int succCount = 0;
			int  failCount= 0;


			try {
				if (couponInDTO.getMemberString()!=null) {
					if (couponInDTO.getMemberString().length() == 1) {
						Long id = Long.parseLong(couponInDTO.getMemberString());
						result =couponUserService.sendOneMemberCoupon(id, couponId, null,
								ActivityEnum.RedPackageGetType.DirectedRelease.getType(),null,null);
						if(result ==1){
							succCount += 1;
						}else if(result ==-1){
							msgMemberId1 += id+",";
							msgMessage1="优惠券已经结束,,会员号为:";
						}else if (result ==-2){
							msgMemberId2 += id+",";
							msgMessage2="优惠券已经领取完成,会员号为:";
						}else if (result ==-3){
							msgMemberId3 += id+",";
							msgMessage3="优惠券已经过期,会员号为:";
						}else if (result ==-4){
							msgMemberId4 += id+",";
							msgMessage4="过了有效结束时间,会员号为:";
						}else if (result ==-5){
							msgMemberId5 += id+",";
							msgMessage5="超过最大领取张数,会员号为:";
						}
						if(result ==-1 ||result ==-2 ||result ==-3 ||result ==-4 ||result ==-5){
							failCount +=1;
							message0=",错误原因:";
						}


					} else {
						String[] ids = couponInDTO.getMemberString().split(",");
						for (String id : ids) {
							result= couponUserService.sendOneMemberCoupon(Long.parseLong(id), couponId, null,
									ActivityEnum.RedPackageGetType.DirectedRelease.getType(),null,null);
							if(result ==1){
								succCount += 1;
							}else if(result ==-1){
								msgMemberId1 += id+",";
								msgMessage1="优惠券已经结束,,会员号为:";
							}else if (result ==-2){
								msgMemberId2 += id+",";
								msgMessage2="优惠券已经领取完成,会员号为:";
							}else if (result ==-3){
								msgMemberId3 += id+",";
								msgMessage3="优惠券已经过期,会员号为:";
							}else if (result ==-4){
								msgMemberId4 += id+",";
								msgMessage4="过了有效结束时间,会员号为:";
							}else if (result ==-5){
								msgMemberId5 += id+",";
								msgMessage5="超过最大领取张数,会员号为:";
							}
							if(result ==-1 ||result ==-2 ||result ==-3 ||result ==-4 ||result ==-5){
								failCount +=1;
								message0=",错误原因:";
							}

						}
					}
				} else {//按条件选
					List<Long> memberList= memberService.getMembersByCoupon(couponInDTO);
					for (Long id : memberList) {
						result= couponUserService.sendOneMemberCoupon(id, couponId, null, getType,null,null);
						if(result ==1){
							succCount += 1;
						}else if(result ==-1){
							msgMemberId1 += id+",";
							msgMessage1="优惠券已经结束,,会员号为:";
						}else if (result ==-2){
							msgMemberId2 += id+",";
							msgMessage2="优惠券已经领取完成,会员号为:";
						}else if (result ==-3){
							msgMemberId3 += id+",";
							msgMessage3="优惠券已经过期,会员号为:";
						}else if (result ==-4){
							msgMemberId4 += id+",";
							msgMessage4="过了有效结束时间,会员号为:";
						}else if (result ==-5){
							msgMemberId5 += id+",";
							msgMessage5="超过最大领取张数,会员号为:";
						}
						if(result ==-1 ||result ==-2 ||result ==-3 ||result ==-4 ||result ==-5){
							failCount +=1;
							message0=",错误原因:";
						}

					}
				}

			}catch (Exception ae){
				logger.error(ae);
			}

			if(msgMemberId1 !="" && msgMemberId1 !=null  ){
				couponOutDTO.setMsgMemberId1(msgMemberId1.substring(0,msgMemberId1.length()-1));
			}else{
				couponOutDTO.setMsgMemberId1("");
			}
			if(msgMemberId2 !="" && msgMemberId2 !=null ) {
				couponOutDTO.setMsgMemberId2(msgMemberId2.substring(0, msgMemberId2.length() - 1));
			}else{
				couponOutDTO.setMsgMemberId2("");
			}
			if(msgMemberId3 !="" && msgMemberId3 !=null ) {
				couponOutDTO.setMsgMemberId3(msgMemberId3.substring(0, msgMemberId3.length() - 1));
			}else{
				couponOutDTO.setMsgMemberId3("");
			}
			if(msgMemberId4 !="" && msgMemberId4 !=null ) {
				couponOutDTO.setMsgMemberId4(msgMemberId4.substring(0, msgMemberId4.length() - 1));
			}else{
				couponOutDTO.setMsgMemberId4("");
			}
			if(msgMemberId5 !="" && msgMemberId5 !=null ) {
				couponOutDTO.setMsgMemberId5(msgMemberId5.substring(0, msgMemberId5.length() - 1));
			}else{
				couponOutDTO.setMsgMemberId5("");
			}
			if(msgMessage1 !="" && msgMessage1 !=null  ){
				couponOutDTO.setMsgMessage1(msgMessage1);
			}else{
				couponOutDTO.setMsgMessage1("");
			}
			if(msgMessage2 !="" && msgMessage2 !=null ) {
				couponOutDTO.setMsgMessage2(msgMessage2);
			}else{
				couponOutDTO.setMsgMessage2("");
			}
			if(msgMessage3 !="" && msgMessage3 !=null ) {
				couponOutDTO.setMsgMessage3(msgMessage3);
			}
			else{
				couponOutDTO.setMsgMessage3("");
			}
			if(msgMessage4 !="" && msgMessage4 !=null ) {
				couponOutDTO.setMsgMessage4(msgMessage4);
			}else{
				couponOutDTO.setMsgMessage4("");
			}
			if(msgMessage5 !="" && msgMessage5 !=null ) {
				couponOutDTO.setMsgMessage5(msgMessage5);
			}else{
				couponOutDTO.setMsgMessage5("");
			}
			if(message0 !="" && message0 !=null){
				couponOutDTO.setMessage0(message0);
			}else{
				couponOutDTO.setMessage0("");
			}

			couponOutDTO.setSuccCount(succCount);
			couponOutDTO.setFailCount(failCount);



			couponOutDTO.setErrorCode(Rescode.SUCCESS, "操作成功");

			String content ="会员批量发优惠券";
			sysLogService.addSysLog(couponInDTO.getUserId(),couponInDTO.getUserName(),null,content,couponInDTO.getGroupId());

			return couponOutDTO;
		} catch (Exception e) {
			couponOutDTO.setErrorCode("-1", "处理出现异常");
			e.printStackTrace();
		}
		couponOutDTO.setErrorCode("-2", "处理失败");
		return null;
	}
	
	
	/**
	 * 交易记录列表查询
	 * @param memberTradeDetailInDTO
	 * @param pageDTO
	 * @return
	 */
	@RequestMapping(value="/tradeDetailList")
	@ResponseBody
	public MemberListOutDTO  tradeDetailList(MemberTradeDetailInDTO memberTradeDetailInDTO,PageDTO pageDTO){
		MemberListOutDTO memberListOutDTO=null;
		try {
			Page page=Tools.pageEmtity(pageDTO);
			
			//查询会员信息
			MemberConsumeOutDTO memberConsumeOutDTO= memberService.queryMemberConsumeData(memberTradeDetailInDTO);
			
			//获取会员交易列表信息
			memberListOutDTO = memberService.queryTradeDetail(memberTradeDetailInDTO,page);			
			memberListOutDTO.setMemberConsumeOutDTO(memberConsumeOutDTO);
			memberListOutDTO.setPage(page); 
			memberListOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
			return memberListOutDTO;
		} catch (ApplicationException ae) { 
			memberListOutDTO.setErrorCode(ae.errCode, ae.errMsg);
			ae.printStackTrace();
		}
		 catch (Exception e) {
			 memberListOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
			e.printStackTrace();
		}
		return memberListOutDTO;
	}

	/**
	 * 会员积分列表查询
	 * @param memberBonusLogInDTO
	 * @param pageDTO
	 * @return
	 */
	@RequestMapping(value="/memberBonusLogList")
	@ResponseBody
	public MemberListOutDTO  memberBonusLogList(MemberBonusLogInDTO memberBonusLogInDTO, PageDTO pageDTO){
		MemberListOutDTO memberListOutDTO=null;
		try {
			Page page=Tools.pageEmtity(pageDTO);
			Member member=new Member();
			member.setId(memberBonusLogInDTO.getMemberId());
			//查询会员信息
			Member members= memberService.findMemberById(member);
			//获取会员交易列表信息
			memberListOutDTO = memberService.queryMemberBonusList(memberBonusLogInDTO,page);
			memberListOutDTO.setMember(members);
			memberListOutDTO.setPage(page);
			memberListOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
			return memberListOutDTO;
		} catch (ApplicationException ae) {
			memberListOutDTO.setErrorCode(ae.errCode, ae.errMsg);
			ae.printStackTrace();
		}
		 catch (Exception e) { 
			 memberListOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
			e.printStackTrace();
		}
		return memberListOutDTO;
	}		
	
	
	@RequestMapping(value="/detaillist")
	@ResponseBody
	public MemberListOutDTO  detaillistMember(MemberInDTO memberInDTO,PageDTO pageDTO){
		MemberListOutDTO memberListOutDTO=new MemberListOutDTO();
		try {
			Page page=Tools.pageEmtity(pageDTO);
			//取得所有会员等级
			memberListOutDTO.setMemberGradeList(memberGradeService.getGradeList(memberInDTO));
			
			//获取会员列表列表信息
			List<MemberOutDTO> memberOutDTOList = memberService.getMemberList(memberInDTO,page);			
			memberListOutDTO.setMemberOutDTOList(memberOutDTOList);
			memberListOutDTO.setPage(page); 
			memberListOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
			
			
			
			return memberListOutDTO;
		} catch (ApplicationException ae) { 
			memberListOutDTO.setErrorCode(ae.errCode, ae.errMsg);
			ae.printStackTrace();
		}
		 catch (Exception e) { 
			 memberListOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
			e.printStackTrace();
		}
		return memberListOutDTO;
	}		
	
	
}
